package com.net.xpay.alipay.safePay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.msg.AlipayMsgClient;
import com.net.xpay.core.helper.EnvironmentHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;

@Slf4j
@Configuration
public class SafePayAlipayConfig {

    @Autowired
    private SafePayAlipayProperties safePayAlipayProperties;

    @Autowired
    private EnvironmentHelper environmentHelper;

    @Autowired
    private ResourceLoader resourceLoader;

    /**
     * 安全付支付宝客户端
     */
    @Bean
    public PoseidonSafePayAlipayClient xpaySafePayAlipayClient() throws AlipayApiException {
        if (!Boolean.TRUE.equals(safePayAlipayProperties.getEnable())) {
            return new PoseidonSafePayAlipayClient(null, 0);
        }
        String url = safePayAlipayProperties.getAlipayTopUrl();
        String appId = safePayAlipayProperties.getAlipayAppId();
        String appPrivateKey = safePayAlipayProperties.getAppPrivateKey();
        String rootCertPath = safePayAlipayProperties.getRootCertPath();
        String certPath = safePayAlipayProperties.getCertPath();
        String alipayPublicCertPath = safePayAlipayProperties.getAlipayPublicCertPath();

        //本地开发环境 证书路径需要读取本地路径
//        if (environmentHelper.isDev()) {
//            String dirname;
//            try {
//                dirname = resourceLoader.getResource("classpath:cert/safe-pay").getURL().getFile();
//            } catch (IOException e) {
//                log.error("get cert path error", e);
//                throw new RuntimeException("get cert path error");
//            }
//            rootCertPath = dirname + "/alipayRootCert.crt";
//            certPath = dirname + "/appCertPublicKey_" + safePayAlipayProperties.getAlipayAppId() + ".crt";
//            alipayPublicCertPath = dirname + "/alipayCertPublicKey_RSA2.crt";
//        }

        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(url);
        certAlipayRequest.setAppId(appId);
        certAlipayRequest.setPrivateKey(appPrivateKey);
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("GBK");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(certPath);
        certAlipayRequest.setAlipayPublicCertPath(alipayPublicCertPath);
        certAlipayRequest.setRootCertPath(rootCertPath);
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);

        return new PoseidonSafePayAlipayClient(alipayClient, -1);
    }

    @Bean
    public PoseidonSafePayAlipayMsgClient xpaySafePayAlipayMsgClient() throws Exception {
        if (!Boolean.TRUE.equals(safePayAlipayProperties.getEnable())) {
            return new PoseidonSafePayAlipayMsgClient();
        }
        String serverHost = safePayAlipayProperties.getServerHost();
        String appId = safePayAlipayProperties.getAlipayAppId();
        String appPrivateKey = safePayAlipayProperties.getAppPrivateKey();
        String rootCertPath = safePayAlipayProperties.getRootCertPath();
        String certPath = safePayAlipayProperties.getCertPath();
        String alipayPublicCertPath = safePayAlipayProperties.getAlipayPublicCertPath();

        //本地开发环境 证书路径需要读取本地路径
//        if (environmentHelper.isDev()) {
//            String dirname;
//            try {
//                dirname = resourceLoader.getResource("classpath:cert/safe-pay").getURL().getFile();
//            } catch (IOException e) {
//                log.error("get cert path error", e);
//                throw new RuntimeException("get cert path error");
//            }
//            rootCertPath = dirname + "/alipayRootCert.crt";
//            certPath = dirname + "/appCertPublicKey_" + safePayAlipayProperties.getAlipayAppId() + ".crt";
//            alipayPublicCertPath = dirname + "/alipayCertPublicKey_RSA2.crt";
//        }

        AlipayMsgClient alipayMsgClient = AlipayMsgClient.getInstance(appId);

        alipayMsgClient.setConnector(serverHost);
        alipayMsgClient.setSecurityCertConfig("RSA2", appPrivateKey, certPath, alipayPublicCertPath, rootCertPath);
        alipayMsgClient.setCharset("UTF-8");

        PoseidonSafePayAlipayMsgClient client = new PoseidonSafePayAlipayMsgClient();
        client.setAlipayMsgClient(alipayMsgClient);


        return client;
    }
}
